package com.wawei.seeds.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.me.MeHeroPortraitActivity;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.utils.WalnutUtils;

import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by t27891 on 15/6/22.
 */
public class WalnutAppointmentFragmentAdapter extends SeedsBaseAdapter<WalnutMainEvent>{

    public WalnutAppointmentFragmentAdapter(Context context){
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder;
        if(null == view){
            view = LayoutInflater.from(context).inflate(R.layout.fragment_walnut_me_appointment_item, parent, false);
            viewHolder = new ViewHolder(view);
            viewHolder.heroLogo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showHeroDetail(v, (WalnutMainEvent)getItem(position));
                }
            });
            view.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder)view.getTag();
        }

        WalnutMainEvent item = (WalnutMainEvent)getItem(position);
        viewHolder.eventTitle.setText(item.title);
        viewHolder.appointmentStatus.setText(WalnutUtils.transformServiceStatus(context, item.bookingState));

        if(null == item.publisherHeadPortrait || item.publisherHeadPortrait.length() < 1){
            imageLoader.loadImageByResources(R.drawable.walnut_logo, viewHolder.heroLogo, true, false);
        }
        else {
            imageLoader.loadImage(item.publisherHeadPortrait, viewHolder.heroLogo, true, false);
        }

        return view;
    }

    private void showHeroDetail(View v, WalnutMainEvent item){
        Intent intent = new Intent(v.getContext(), MeHeroPortraitActivity.class);
        intent.putExtra("item", item.publisherId);

        v.getContext().startActivity(intent);
    }

    static class ViewHolder{
        @InjectView(R.id.item_appointment_hero_logo)ImageView heroLogo;
        @InjectView(R.id.item_appointment_event_title)TextView eventTitle;
        @InjectView(R.id.item_appointment_status)TextView appointmentStatus;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
